package com.gusha.controller;

import java.math.BigInteger;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.gusha.config.SysOperateLog;
import com.gusha.dao.SysEnjoyDao;
import com.gusha.model.SysEnjoy;
import com.gusha.service.SysEnjoyService;
import com.gusha.utils.Constants;
import com.gusha.utils.FileUtils;
import com.gusha.utils.JsonResult;
import com.gusha.utils.WebValueUtil;

@Controller
@RequestMapping("/gx/sysEnjoy")
public class SysEnjoyController {
		// 注入Dao
		@Autowired
		private SysEnjoyDao sysEnjoyDao;
		// 注入Service
		@Autowired
		private SysEnjoyService sysEnjoyService;
			
		// 查询列表
		@RequestMapping("list")
		@SysOperateLog(operate = "查看共享管理列表")
		public String list(Model model, HttpServletRequest request) {
		//请求参数
		String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
		String enName = WebValueUtil.getWebValue(request, "enName");
		String enType = WebValueUtil.getWebValue(request, "enType");
	
		//参数封装
		int pageNo = 0;
		if (StringUtils.isNotBlank(pageIndex)) {
		pageNo = Integer.parseInt(pageIndex);
		}
		SysEnjoy sysEnjoy=new SysEnjoy();
        if (StringUtils.isNotBlank(enName)) {
        	sysEnjoy.setEnName(enName);
        }
        
        if (StringUtils.isNotBlank(enType)) {
        	sysEnjoy.setEnType(new BigInteger(enType));
        }
        
		//获取分页数据
		Sort sort = new Sort(Sort.Direction.DESC, "addTime");
		Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
		Page<SysEnjoy> page = sysEnjoyService.findAll(pageable, sysEnjoy);
		model.addAttribute("page", page);
		return "/admin/sysEnjoy/list";
		}
		// 进入添加页面
	    @RequestMapping("toAdd")
	    public String toAdd(Model model) {
	        return "/admin/sysEnjoy/form";
	    }
	    // 保存
        @RequestMapping("save")
        @ResponseBody
        @SysOperateLog(operate = "保存共享信息")
        public JsonResult save(SysEnjoy sysEnjoy,/*@RequestParam("fileName") MultipartFile file,*/HttpServletRequest request) {
        	JsonResult result = new JsonResult(false);
        /*	
        	String req= file.getOriginalFilename();
        	String videoPath = Constants.getFilePath("info")+req;
			String localPath = Constants.IMAGE_LOCAL_PATH+Constants.getFilePath("info");
			FileUtils.upload(file, localPath, req);
			sysEnjoy.setEnVideo(videoPath);
		*/
        	sysEnjoy.setIsDelete(new BigInteger("0"));
        	sysEnjoyDao.saveAndFlush(sysEnjoy);
            result.setFlag(true);
            result.setMsg("保存成功");
            return result;
        }
        
     // 删除
        @RequestMapping("delete")
        @ResponseBody
        @SysOperateLog(operate = "删除图片信息")
        public JsonResult delete(BigInteger id) {
            JsonResult result = new JsonResult(false,"删除失败");
            if (sysEnjoyDao.existsById(id)) {
            	SysEnjoy sysEnjoy = sysEnjoyDao.getOne(id);
            	sysEnjoy.setIsDelete(new BigInteger("1"));
            	sysEnjoyDao.saveAndFlush(sysEnjoy);
                result.setFlag(true);
                result.setMsg("删除成功");
            }else{
                result.setFlag(false);
                result.setMsg("该对象信息不存在");
                return result;
            }
            return result;
        }
        
        // 恢复
        @RequestMapping("recovery")
        @ResponseBody
        @SysOperateLog(operate = "恢复资讯信息")
        public JsonResult recovery(BigInteger id) {
            JsonResult result = new JsonResult(false,"恢复失败");
            if (sysEnjoyDao.existsById(id)) {
            	SysEnjoy sysEnjoy = sysEnjoyDao.getOne(id);
            	sysEnjoy.setIsDelete(new BigInteger("0"));
                sysEnjoyDao.saveAndFlush(sysEnjoy);
                result.setFlag(true);
                result.setMsg("恢复成功");
            }else{
                result.setFlag(false);
                result.setMsg("该对象信息不存在");
                return result;
            }
            return result;
        }
      //进入编辑页面
        @RequestMapping("toEdit")
        @SysOperateLog(operate = "进入共享编辑页面")
        public String toEdit(Model model, HttpServletRequest request) {

            //请求参数
            String id = WebValueUtil.getWebValue(request, "id");

            if (StringUtils.isNotBlank(id)) {
                if(sysEnjoyDao.existsById(new BigInteger(id))) {
                	SysEnjoy sysEnjoy = sysEnjoyDao.getOne(new BigInteger(id));
                    model.addAttribute("obj", sysEnjoy);
                }
            }        
            return "/admin/sysEnjoy/form";
        }
}
